package com.barbarossa.vod.utils;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

//import android.util.Log;

/**
 * 
 * @author Cliff <uses-permission android:name="android.permission.INTERNET" />
 */

public class PageAnalyzer {

	protected URI indexPage = null;
	protected URI currentPageUri = null;
	protected String currentPageText = null;

	protected String defaultContentType = "example";
	protected String defaultCharacterEncoding = "UTF-8";

//	private final String TAG = "PageAnalyzer";
	private final String CONTENT_TYPE_TEXT = "text";
	
	protected OnGrabListener grabListener = null;
	protected OnAnalyzeListener analyzeListener = null;
	
	public URI getIndexPage() {
		return indexPage;
	}

	public void setIndexPage(URI indexPage) {
		this.indexPage = indexPage;
	}

	public URI getCurrentPageUri() {
		return currentPageUri;
	}

	public void setCurrentPageUri(URI currentPage) {
		this.currentPageUri = currentPage;
	}

	public String getCurrentPageText() {
		return currentPageText;
	}

	public void setCurrentPageText(String currentPageText) {
		this.currentPageText = currentPageText;
	}

	public String getDefaultContentType() {
		return defaultContentType;
	}

	public void setDefaultContentType(String defaultContentType) {
		this.defaultContentType = defaultContentType;
	}

	public String getDefaultCharacterEncoding() {
		return defaultCharacterEncoding;
	}

	public void setDefaultCharacterEncoding(String defaultCharacterEncoding) {
		this.defaultCharacterEncoding = defaultCharacterEncoding;
	}

	public OnGrabListener getOnGrabListener() {
		return grabListener;
	}

	public void setOnGrabListener(OnGrabListener listener) {
		this.grabListener = listener;
	}

	public OnAnalyzeListener getOnAnalyzeListener() {
		return analyzeListener;
	}

	public void setOnAnalyzeListener(OnAnalyzeListener listener) {
		this.analyzeListener = listener;
	}

	protected String grabWebPageText(URI uri) {
		if (null == uri)
			return null;
		
		HttpGet httpRequest = new HttpGet(uri);
		String strResult = null;
		String strContentType = null;
		String strCharacterEncoding = null;
		HttpEntity entity = null;
		long startTimeMillis = 0;
		long endTimeMillis = 0;
		try {
			startTimeMillis = System.currentTimeMillis();
			System.out.println("grabWebPageText start at " + startTimeMillis
					+ " => " + uri);
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				entity = httpResponse.getEntity();
				strContentType = entity.getContentType().getValue();
				if (null == strContentType)
					strContentType = this.getDefaultContentType();

				if (strContentType.toLowerCase().startsWith(CONTENT_TYPE_TEXT)) {
					strCharacterEncoding = EntityUtils
							.getContentCharSet(httpResponse.getEntity());
					if (null == strCharacterEncoding)
						strCharacterEncoding = getDefaultCharacterEncoding();
					strResult = EntityUtils.toString(httpResponse.getEntity(),
							strCharacterEncoding);
				}
			}
			endTimeMillis = System.currentTimeMillis();
			System.out.println("grabWebPageText end at " + endTimeMillis
					+ ", used " + (endTimeMillis - startTimeMillis));
			// Log.d(TAG, "grabWebPageText end at " + endTimeMillis + ", used "
			// + (endTimeMillis - startTimeMillis));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return strResult;
	}

	protected boolean grabPage(URI uri) {
		if (null != grabListener)
			grabListener.onGrabStart();

		if (null == uri)
			uri = getIndexPage();

		String strPageText = grabWebPageText(uri);
		if (null != strPageText) {
			setCurrentPageUri(uri);
			setCurrentPageText(strPageText);
			if (null != grabListener)
				grabListener.onGrabEnd(true);
			return true;
		}

		if (null != grabListener)
			grabListener.onGrabEnd(false);
		return false;
	}

	protected void clearCurrentPage() {
		setCurrentPageUri(null);
		setCurrentPageText(null);
	}

	public URI getUri(String strUri) {
		URI uri = null;
		try {
			if (null != strUri)
				uri = new URI(strUri);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return uri;
	}
	
	public interface OnGrabListener {
		void onGrabStart();
		void onGrabEnd(boolean succeeded);
	}

	public interface OnAnalyzeListener {
		void onAnalyzeStart();
		void onAnalyzeEnd(boolean succeeded);
	}

}
